Skip to content

HTTP POST请求 - HttpPost

函数简介

发送简单的HTTP POST请求,返回响应体字符串。

接口名称

HttpPost

DLL调用

c
const char* HttpPost(int64_t instance, const char* url, const char* body, const char* content_type);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
url字符串完整URL。
body字符串请求体内容。
content_type字符串Content-Type,例如"application/json"。

使用场景

场景content_typebody 示例
提交 JSONapplication/json{"username":"ola","level":10}
表单登录application/x-www-form-urlencodeduser=ola&pass=123456
上传 XMLapplication/xml<root><name>ola</name></root>
纯文本text/plainhello world

如需附加 Authorization、Cookie 等请求头,请改用 HttpRequestEx

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// POST JSON
string resp = ola.HttpPost(
    "https://httpbin.org/post",
    "{\"username\":\"ola\",\"level\":10}",
    "application/json"
);
// 表单提交示例:
// ola.HttpPost("https://httpbin.org/post", "user=ola&pass=123", "application/x-www-form-urlencoded");
csharp
using OLAPlug;

var ola = new OLAPlugServer();
string resp = ola.HttpPost(
    "https://httpbin.org/post",
    "{\"username\":\"ola\",\"level\":10}",
    "application/json"
);
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
resp = ola.HttpPost(
    "https://httpbin.org/post",
    '{"username":"ola","level":10}',
    "application/json"
)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
String resp = ola.HttpPost(
    "https://httpbin.org/post",
    "{\"username\":\"ola\",\"level\":10}",
    "application/json"
);
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
resp := ola.HttpPost("https://httpbin.org/post", "{\"username\":\"ola\",\"level\":10}", "application/json")
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let resp = ola.http_post("https://httpbin.org/post", "{\"username\":\"ola\",\"level\":10}", "application/json");
cpp
var ola = com("OlaPlug.OlaSoft")
var resp = ola.HttpPost("https://httpbin.org/post", "{\"username\":\"ola\",\"level\":10}", "application/json")
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
resp = ola.HttpPost("https://httpbin.org/post", "{\"username\":\"ola\",\"level\":10}", "application/json")
text
.局部变量 ola, OLAPlug
ola.创建 ()
resp = ola.HttpPost ("https://httpbin.org/post", "{\"username\":\"ola\",\"level\":10}", "application/json")
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var resp = ola.HttpPost("https://httpbin.org/post", "{\"username\":\"ola\",\"level\":10}", "application/json");
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 resp = ola.HttpPost("https://httpbin.org/post", "{\"username\":\"ola\",\"level\":10}", "application/json")
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
string resp = ola.HttpPost("https://httpbin.org/post", "{\"username\":\"ola\",\"level\":10}", "application/json");

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long respPtr = HttpPost(instance, "https://httpbin.org/post", "{\"username\":\"ola\"}", "application/json");
if (respPtr != 0) {
    char resp[4096] = {0};
    GetStringFromPtr(respPtr, resp, sizeof(resp));
    FreeStringPtr(respPtr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long HttpPost(long ola, string url, string body, string content_type);

long instance = CreateCOLAPlugInterFace();
long respPtr = HttpPost(instance, "https://httpbin.org/post", "{\"username\":\"ola\"}", "application/json");
if (respPtr != 0) {
    StringBuilder resp = new StringBuilder(GetStringSize(respPtr) + 1);
    GetStringFromPtr(respPtr, resp, resp.Capacity);
    FreeStringPtr(respPtr);
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
respPtr = ola.HttpPost(instance, b"https://httpbin.org/post", b'{"username":"ola"}', b"application/json")
if respPtr:
    buf = create_string_buffer(4096)
    ola.GetStringFromPtr(respPtr, buf, 4096)
    ola.FreeStringPtr(respPtr)

返回值

返回值说明
(返回值)字符串指针,返回响应体内容,失败返回NULL。需调用 FreeStringPtr 释放内存。

注意事项

项目说明
释放内存返回的字符串需要调用 FreeStringPtr 释放内存。
content_type必须与body内容匹配content_type必须与body内容匹配。
常用Content-Typeapplication/jsonapplication/x-www-form-urlencodedapplication/xmltext/plain
如需自定义请求头或Cookie如需自定义请求头或Cookie,请使用 HttpRequestEx